package org.breeze.option.demos.task;

import org.breeze.option.demos.service.OptionService;
import org.breeze.option.demos.service.WebSocketService;
import org.breeze.option.demos.utils.HttpsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;

@Component
public class TimeTask {
    @Resource
    private OptionService optionService;

    private final WebSocketService webSocketService;

    private final String[] optionObjects = new String[]{"ho", "io", "mo"};

    /**
     * 行情接口，最后需要加参数，合约名，如io2412
     */
    private String url_300 = "https://stock.finance.sina.com.cn/futures/api/openapi.php/OptionService.getOptionData?callback=jQuery17208149031601895427_1732860072778&type=futures&product=io&exchange=cffex&pinzhong=";
    private String url_50 = "https://stock.finance.sina.com.cn/futures/api/openapi.php/OptionService.getOptionData?callback=jQuery172036428100958666687_1733196563298&type=futures&product=ho&exchange=cffex&pinzhong=";
    private String url_1000 = "https://stock.finance.sina.com.cn/futures/api/openapi.php/OptionService.getOptionData?callback=jQuery17209874071553685329_1733196811472&type=futures&product=mo&exchange=cffex&pinzhong=";

    @Autowired
    public TimeTask(WebSocketService webSocketService) {
        this.webSocketService = webSocketService;
    }

    /**
     * 或许新浪行情接口数据流程
     * @author chuyz
     * @date 2024/12/1 15:52
     */
    private void sinoDataService() {
        //获取当前3个月的合约日期
        String[] dates = getOptionDate(3);
        for(String date: dates) {
            for(String option_object: optionObjects) {
                String name = option_object+date;
                String data = "";
                switch (option_object) {
                    case "ho":
                        data = HttpsUtils.doGet(url_50+name, "utf-8");
                        break;
                    case "io":
                        data = HttpsUtils.doGet(url_300+name, "utf-8");
                        break;
                    case "mo":
                        data = HttpsUtils.doGet(url_1000+name, "utf-8");
                        break;
                }
                String[] datas = data.split("\\(");
                data = datas[1].substring(0, datas[1].length() - 2);
                optionService.handleSinaData(data, option_object);
            }
        }
        //算出总价
        //session获取当前页面的状态，获取当前页面显示的标的物，合约日期
        //向页面推送实时合约具体信息，总价信息（包括总价，总持仓），将两个信息提取，数据组包
        //推送echarts信息，
        //具体合约数据和总价数据存入数据库
    }

    /**
     * 返回需要获取的合约日期
     * @param num 需要获取的月份数量
     * @return 如['2412', '2501', '2502']
     * @author chuyz
     * @date 2024/12/1 16:18
     */
    private String[] getOptionDate(int num) {
        LocalDate currentDate = LocalDate.now();

        String year = Integer.toString(currentDate.getYear()).substring(2,4);
        String month = Integer.toString(currentDate.getMonthValue());

        String[] result = new String[num];
        result[0] = year + month;

        for(int i = 1; i <= (num - 1); i++) {
            int nextMonth = currentDate.getMonthValue() + i;
            int nextYear = currentDate.getYear();
            if(nextMonth > 12) {
                nextMonth -= 12;
                nextYear += 1;
            }
            result[i] = Integer.toString(nextYear).substring(2, 4) + (nextMonth < 10 ? "0" + nextMonth :Integer.toString(nextMonth));
        }

        return result;
    }

    /**
     * 定时任务1，9点到11点，1点到3点
     * @author chuyz
     * @date 2024/12/1 15:51

     */
    @Scheduled(cron = "*/30 * 9-10,13-15 * * 1-5")
    public void getSinoData() {
        sinoDataService();
    }

    /**
     * 定时任务2,11点到11点半
     * @author chuyz
     * @date 2024/12/1 15:51
     */
    @Scheduled(cron = "*/30 0-40 11 * * 1-5")
//    @Scheduled(cron = "*/2 * * * * *")
    public void getSinoData1() {
        sinoDataService();
    }
}
